//
//  ParsingDataMgr.m
//  cherryPicker
//
//  Created by Young Soo Yun on 12. 1. 2..
//  Copyright (c) 2012 Happy. All rights reserved.
//

#import "ParsingDataMgr.h"

#import "StringUtils.h"


@implementation ParsingDataMgr

+ (NSArray*)parsingResults:(NSString*)data{

    CardListDB* card = [self searchCardName:data];
    // no delimiter
    if (card == nil) {
        return nil;
    }
    
    NSMutableArray* arrResult = [[NSMutableArray alloc] init];
    
    [arrResult addObject:card._CardName];
    [arrResult addObject:card._Delimiter];
    
    NSArray* arrData = [self divideSMSStr:data];
    
    NSString* strPrice = nil;
    NSString* strDate = nil;
    
    for (int i = 0; i < arrData.count; i++) {

        // 금액
        if([StringUtils isContainStr:[arrData objectAtIndex:i] chekStr:@"원"] && strPrice == nil){
            strPrice = [arrData objectAtIndex:i];
            
            // 가격 뒤에 스페이스가 없을 경우를 대비하여 자름
            NSRange wonRange = [strPrice rangeOfString:@"원"];
            strPrice = [strPrice substringToIndex:wonRange.location];
        }

        // 날짜
        if([StringUtils isContainStr:[arrData objectAtIndex:i] chekStr:@"/"]){
            NSRange range = [[arrData objectAtIndex:i] rangeOfString:@"/"];
            
            strDate = [[arrData objectAtIndex:i] substringWithRange:NSMakeRange(range.location - 2, 5)];
        }
    }

    [arrResult addObject:(strDate!=nil)?strDate:@""];
    [arrResult addObject:(strPrice!=nil)?strPrice:@"0 원"];

    return arrResult;
}

+ (CardListDB*)searchCardName:(NSString*)data{
    
    NSArray* arrCardList = [CherryPickerDB selectFromCardList];
    
    for (int i = 0; i < arrCardList.count; i++) {
        CardListDB* card = (CardListDB*)[arrCardList objectAtIndex:i];
        
        // check data is contain string
        if ([StringUtils isContainStr:data chekStr:card._Delimiter]) {
            
            // check 1 more (equal or not)
            NSArray* arrData = [self divideSMSStr:data];
            for (int i = 0; i < arrData.count; i++) {
                if ([[arrData objectAtIndex:i] isEqualToString:card._Delimiter]) {
                    return card;
                }
            }
        }
    }
    
    return nil;
}

+ (NSArray*)divideSMSStr:(NSString*)data{
    NSMutableArray* arrResult = [[NSMutableArray alloc] init];
    
    NSArray* arrData = [data componentsSeparatedByString:@"\n"];
    
    for (int i = 0; i < arrData.count; i++) {
        NSArray* arrSpaceData = [[arrData objectAtIndex:i] componentsSeparatedByString:@" "];
        
        if (arrSpaceData.count > 1) {
            for (int j = 0 ; j < arrSpaceData.count; j++) {
                if (![[arrSpaceData objectAtIndex:j] isEqualToString:@""] && [arrSpaceData objectAtIndex:j] != nil) {
                    [arrResult addObject:[arrSpaceData objectAtIndex:j]];
                }
            }
        }else{
            [arrResult addObject:[arrData objectAtIndex:i]];
        }
    }

    return arrResult;
}

+ (NSArray*)delimiterList:(NSString*)data{
    
    NSMutableArray* arrResult = [[NSMutableArray alloc] init];
    NSArray* arrData = [self divideSMSStr:data];
    
    for (int i = 0; i < arrData.count; i++) {
        if (![StringUtils isContainStr:[arrData objectAtIndex:i] chekStr:@"원"]) {
            [arrResult addObject:[arrData objectAtIndex:i]];
        }
    }
    
    return arrResult;
}

@end